package com.zhu.ttwords.view.activity;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhu.ttwords.R;
import com.zhu.ttwords.bean.MainBean;
import com.zhu.ttwords.presenter.MainPresenter;

public class MainActivity extends ActionBarActivity implements IMainActivity {
    MainPresenter presenter;
    //views;
    ImageView photo;
    TextView total;
    TextView today;
    TextView tomorrow;

    private long firstTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        photo = (ImageView) this.findViewById(R.id.activity_main_photo);
        total = (TextView) this.findViewById(R.id.activity_main_total);
        today = (TextView) this.findViewById(R.id.activity_main_today);
        tomorrow = (TextView) this.findViewById(R.id.activity_main_tomorrow);
        presenter = new MainPresenter(this);
        presenter.init();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent intent = new Intent(this,SettingActivity.class);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) { // 如果两次按键时间间隔大于2秒，则不退出
                    Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    firstTime = secondTime;// 更新firstTime
                    return true;
                } else { // 两次按键小于2秒时，退出应用
                    this.finish();
                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }
    @Override
    public void setUserData(MainBean bean) {
        total.setText(bean.getTotal());
        today.setText(bean.getToday());
        tomorrow.setText(bean.getTomorrow());
    }


    public void startStudy(View v) {
        Intent intent = new Intent(this, StudyActivity.class);
        startActivity(intent);
    }

    public void startTranslate(View v) {

        Intent intent = new Intent(this, TranslateActivity.class);
        startActivity(intent);
    }

    public void startStatistics(View v) {
        Intent intent = new Intent(this, StatisticsActivity.class);
        startActivity(intent);
    }

}
